import request, { ApiResponse } from '@/utils/service/request'
import { CommontListResp } from '@/api/types'

import type {
  LoginRequest,
  AccountInfo,
  GetAccountsReq,
  CommonUserInfo,
  InsertAccountReq,
  ModifyAccountReq,
  ModifyAccountRoleReq,
} from './model/accountModel'

export enum AccountApi {
  /** 登录接口 */
  LOGIN = '/user-center/user/login',
  /** 获取系统账号列表 */
  GET_ACCOUNT_LIST = '/user-center/user/findUserPageByKey',
  /** 添加新账号 */
  INSERT_ACCOUNT = '/user-center/user/insert',
  /** 修改账号信息 */
  UPDATE_ACCOUNT = '/user-center/user/update',
  /** 修改账号角色 */
  UPDATE_ACCOUNT_ROLE = '/user-center/user/updateUserRole',
  /** 获取特定账号角色列表 */
  GET_ACCOUNT_ROLE_LIST = '/user-center/user/getRolesByUserId',
  /** 删除指定账号 */
  DELETE_ACCOUNT = '/user-center/user/delete',
  /** 批量重置账号密码 */
  RESET_ACCOUNT_PADDWORD = '/user-center/user/updatePasswords',
  /** 账号修改密码 */
  UPDATE_ACCOUNT_PASSWORD = '/user-center/user/updatePassword',
}

export function loginIn(body: LoginRequest) {
  return request.post<ApiResponse<AccountInfo>>(AccountApi.LOGIN, body)
}

export function fetchAccountList(body: GetAccountsReq) {
  return request.post<ApiResponse<CommontListResp<CommonUserInfo>>>(
    AccountApi.GET_ACCOUNT_LIST,
    body
  )
}

export function insertAccount(body: InsertAccountReq) {
  return request.post<ApiResponse<CommonUserInfo>>(
    AccountApi.INSERT_ACCOUNT,
    body
  )
}

export function updateAccount(body: ModifyAccountReq) {
  return request.post<ApiResponse<CommonUserInfo>>(
    AccountApi.UPDATE_ACCOUNT,
    body
  )
}

export function updateAccountRole(body: ModifyAccountRoleReq) {
  return request.post<ApiResponse<null>>(AccountApi.UPDATE_ACCOUNT_ROLE, body)
}

export function deleteAccount(accoundId: string) {
  return request.post<ApiResponse<null>>(AccountApi.DELETE_ACCOUNT, {
    id: accoundId,
  })
}

export function fetchAccountRoleList(accountId?: string) {
  return request.get<ApiResponse<string[]>>(AccountApi.GET_ACCOUNT_ROLE_LIST, {
    params: { id: accountId },
  })
}

export function resetAccountPassword(ids: string[]) {
  return request.post<ApiResponse<null>>(AccountApi.RESET_ACCOUNT_PADDWORD, {
    ids,
  })
}

export function updateAccountPassword(body: {
  password: string
  newPassword: string
}) {
  return request.post<ApiResponse<null>>(
    AccountApi.UPDATE_ACCOUNT_PASSWORD,
    body
  )
}
